import { Injectable, Inject } from '@nestjs/common';
import { RedisClientType } from 'redis';
@Injectable()
export class RedisService {
  constructor(
    @Inject('REDIS_CLIENT')
    private readonly redisClient: RedisClientType,
  ) {}

  async set(key: string, value: string | number, ttl?: number) {
    if (ttl) {
      await this.redisClient.set(key, value, { EX: ttl });
    } else {
      await this.redisClient.set(key, value);
    }
  }

  async get(key: string) {
    return await this.redisClient.get(key);
  }

  async del(key: string) {
    await this.redisClient.del(key);
  }

  // 设置验证码
  async setVerifyCode(email: string, code: string) {
    const key = `verify:${email}`;
    await this.set(key, code, 300); // 5分钟过期
  }

  // 获取验证码
  async getVerifyCode(email: string) {
    const key = `verify:${email}`;
    return await this.get(key);
  }
}
